package com.decent.ejfadmin.equity.price.service.impl;

import cn.decent.component.shiro.service.AdminService;
import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.SupplyProductPrice;
import com.decent.ejfadmin.common.util.DingTalkMessagePush;
import com.decent.ejfadmin.config.PlatConfig;
import com.decent.ejfadmin.config.PlatUrlConfig;
import com.decent.ejfadmin.equity.common.CardPriceInfo;
import com.decent.ejfadmin.equity.common.CardProduct;
import com.decent.ejfadmin.equity.price.dao.SetCardPriceDao;
import com.decent.ejfadmin.equity.price.service.SetCardPriceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * describe:权益产品密价设置
 *
 * @author zhangbin
 * @date 2019/03/19
 */
@Service
public class SetCardPriceServiceImpl implements SetCardPriceService {
    @Autowired
    private SetCardPriceDao setCardPriceDao;
    @Autowired
    private AdminService adminService;
    @Autowired
    private PlatConfig platConfig;
    @Autowired
    private PlatUrlConfig platUrlConfig;
    @Autowired
    private DingTalkMessagePush dingTalkMessagePush;

    /**
     * 查询产品列表
     *
     * @param productType 运营商/卡种
     * @return
     */
    @Override
    public List<CardProduct> queryProductList(Integer productType) {
        if (productType == null) {
            return Collections.emptyList();
        }
        return setCardPriceDao.queryProductList(productType);
    }

    /**
     * 设置权益密价
     *
     * @param cardPriceInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageBean setUpCardPrice(CardPriceInfo cardPriceInfo) throws Exception {
        //删除
        Integer delStatus = 4;
        //参数校验
        MessageBean msg = cardPriceInfo.check(delStatus);
        if (msg != null) {
            return msg;
        }
        //操作者
        cardPriceInfo.setAdmin(adminService.getCurrentAdmin().getName());
        int count = 0;
        int editNum = 0;
        if (cardPriceInfo.getUserIds() != null && cardPriceInfo.getUserIds().size() > 0) {
            //选择会员后,操作权益密价配置
            //修改
            if (Integer.valueOf(0).equals(cardPriceInfo.getStatus())) {
                count = setCardPriceDao.updateCardPrice(cardPriceInfo);
            } else if (Integer.valueOf(1).equals(cardPriceInfo.getStatus())) {
                //新增
                for (String userId : cardPriceInfo.getUserIds()) {
                    for (String productId : cardPriceInfo.getProductIds()) {
                        int result = setCardPriceDao.addCardPrice(cardPriceInfo, userId, productId);
                        count += result;
                    }
                }
            } else if (delStatus.equals(cardPriceInfo.getStatus())) {
                //删除
                count = setCardPriceDao.deleteCardPrice(cardPriceInfo);
            }
        } else {
            //没有选择会员,操作标准模板密价配置
            //查询权益产品
            List<Integer> productIds = setCardPriceDao.querySupplyProducts(StringUtils.join(cardPriceInfo.getProductIds(), ","));
            StringBuilder dingDingContent = new StringBuilder();
            if (Integer.valueOf(0).equals(cardPriceInfo.getStatus())) {
                if (productIds != null && !productIds.isEmpty()) {
                    dingDingContent.append("豆乐卡平台权益产品：\n");
                    for (Integer productId : productIds) {
                        SupplyProductPrice supplyProductPrice = setCardPriceDao.querySupCardPrice(productId);
                        if (supplyProductPrice != null) {
                            dingDingContent.append(supplyProductPrice.getProductName()).append("价格由").append(supplyProductPrice.getMinPrice().setScale(2))
                                    .append("调整为:").append(cardPriceInfo.getMinPrice()).append("\n");
                        }
                    }
                }
            } else if (Integer.valueOf(1).equals(cardPriceInfo.getStatus())) {
                if (productIds != null && !productIds.isEmpty()) {
                    dingDingContent.append("豆乐卡平台权益产品：\n");
                    for (Integer productId : productIds) {
                        SupplyProductPrice supplyProductPrice = setCardPriceDao.queryCardProductName(productId);
                        if (supplyProductPrice != null) {
                            dingDingContent.append(supplyProductPrice.getProductName()).append("、");
                        }
                    }
                    dingDingContent.append("新增价格:").append(cardPriceInfo.getMinPrice());
                }
            } else if (delStatus.equals(cardPriceInfo.getStatus())) {
                if (productIds != null && !productIds.isEmpty()) {
                    dingDingContent.append("豆乐卡平台权益产品：\n");
                    for (Integer productId : productIds) {
                        SupplyProductPrice supplyProductPrice = setCardPriceDao.querySupCardPrice(productId);
                        if (supplyProductPrice != null) {
                            dingDingContent.append(supplyProductPrice.getProductName()).append("价格").append(supplyProductPrice.getMinPrice().setScale(2))
                                    .append("已删除:").append("\n");
                        }
                    }
                }
            }
            if (editNum > 0) {
                dingTalkPush(dingDingContent, adminService.getCurrentAdmin().getName());
            }
        }
        return MessageBean.success("操作成功" + count + "条");
    }

    /**
     * 权益产品密价调整通知
     *
     * @param dingDingContent 通知内容
     * @return 返回
     */
    private void dingTalkPush(StringBuilder dingDingContent, String admin) {
        String content = StringUtils.removeEnd(dingDingContent.toString(), "\n") + "\n操作人:" + admin;
        //钉钉推送
        dingTalkMessagePush.dingDingRobotPushing(content, platUrlConfig.getProductChangeInform());
    }
}
